﻿using B.S.DataBase.MyProject.Domain.HealthMonitoring;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Write.API.Appcation.Commands.HealthMonitoringCommand;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.HealthMonitoringCommandHandler
{
    /// <summary>
    /// 删除血糖
    /// </summary>
    public class DelBloodSugarCommandHandler : IRequestHandler<DelBloodSugarCommand, APIResult<int>>
    {
        /// <summary>
        /// SqlSugar
        /// </summary>
        private readonly SqlSugarContext sqlSugar;

        public DelBloodSugarCommandHandler(SqlSugarContext sqlSugar)
        {
            this.sqlSugar = sqlSugar;
        }
        public async Task<APIResult<int>> Handle(DelBloodSugarCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<int> result = new APIResult<int>();
                var entity = sqlSugar.db.Deleteable<BloodSugar>().Where(x => x.SId == request.SId).ExecuteCommand();
                if (entity > 0)
                {
                    result.Code = APIEnum.删除成功;
                    result.Msg = APIEnum.删除成功.ToString();
                    result.data = 1;
                }
                else
                {
                    result.Code = APIEnum.删除失败;
                    result.Msg = APIEnum.删除失败.ToString();
                    result.data = 0;
                }
                return await Task.FromResult(result);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
